<?php
/**
 * PddSku.php class file.
 * @author wudg <544038230@qq.com>
 * @date 2021/1/15- 上午10:18
 */

namespace common\jobs\copy\tools;


use common\helpers\StringHelper;
use yii\helpers\ArrayHelper;

class PddSku
{


    /**
     * 检查sku数量是否和乘积一致
     * @param array $skuData
     * @param array $skuList
     * @return bool
     */
    public static function checkSkuNum(array $skuData,array $skuList)
    {
        $skuNum = array_map(function ($a){
            return count($a);
        },$skuData);

        //计算sku乘积
        $totalSkuNum = array_product($skuNum);

        if(count($skuList) != $totalSkuNum)
        {
            return false;
        }
        return true;
    }


    /**
     * sku 树状结构
     * @param array $skuData
     * @return array
     */
    public static function formatSkuTree(array $skuData)
    {
        $skuTree = [];
        foreach($skuData as $k => $item)
        {
            if(is_array($item))
            {
                foreach ($item as $i => $val)
                {
                    $skuTree[$k][] = $val['props']['spec_id'];
                }
            }
        }
        return $skuTree;
    }


    /**
     * 自动补全sku乘积，默认按照最低团购价填充
     * @param array $skuData
     * @param array $skuList
     * @param int $type
     * @return array
     * @since 修复计算sku乘积后 增加的值没有排序导致上货后的sku 名称错乱bug 2021-01-31 by wudg
     */
    public static function autoFullSku(array $skuData,array $skuList,int $type = 1)
    {

        $skuTree = self::formatSkuTree($skuData);
        $dekaerSkuList = self::dikaerSku($skuTree);

        $multiPriceArr = ArrayHelper::getColumn($skuList,'multi_price');
        $priceArr = ArrayHelper::getColumn($skuList,'price');
        if($type == 1)
        {
            $price = min($priceArr);
            $multi_price = min($multiPriceArr);
        }else{
            $price = max($priceArr);
            $multi_price = max($multiPriceArr);
        }
        $transImg = ArrayHelper::map($skuList,'spec_id_list','thumb_url');
        $skuIdList = ArrayHelper::getColumn($skuList,'spec_id_list');
        foreach ($dekaerSkuList as $skuListId)
        {
            $inKey = self::checkIdInList($skuListId,$skuIdList);
            if($inKey === false)
            {
                $tmp = [
                    'limit_quantity' => 999999,
                    'out_source_sku_id' => '',
                    'is_onsale' => 0,
                    'quantity' => 0,
                    'out_sku_sn' => '',
                    'spec_id_list' => $skuListId,
                    'weight' => 1000,
                    'multi_price' => $multi_price,
                    'price' => $price,
                ];
                $skuListIdArr = StringHelper::jsonDecode($skuListId);
                foreach ($transImg as $k => $img)
                {
                    $kArr = StringHelper::jsonDecode($k);
                    foreach ($skuListIdArr as $id)
                    {
                        if(in_array($id,$kArr))
                        {
                            $tmp['thumb_url'] = $img;
                            break;
                        }
                    }
                }
                array_push($skuList,$tmp);
            }else{

                $skuList[$inKey]['spec_id_list'] = $skuListId;
            }
        }
        return $skuList;
    }


    /**
     * 判断是否在数组内
     * @param string $ids
     * @param array $idsList
     * @return bool|int|string
     */
    private static function checkIdInList(string $ids,array $idsList)
    {
        $idsArr = StringHelper::jsonDecode($ids);
        sort($idsArr);
        foreach($idsList as $k => $item)
        {
            $itemArr = StringHelper::jsonDecode($item);
            sort($itemArr);

            if($idsArr == $itemArr)
            {
                return $k;
            }
        }
        return false;
    }


    /**
     * 计算sku笛卡尔积
     * @param array $arr
     * @since 优化计算笛卡尔积后的结果，全部按数字大小升序排列
     * @return array|mixed
     */
    private static function dikaerSku(array $arr){
        $result = array_shift($arr);
        while($arr2 = array_shift($arr)){
            $arr1 = $result;
            $result = [];
            foreach($arr1 as $v){
                foreach($arr2 as $v2){
                    $result[] = $v.','.$v2;
                }
            }
        }
        $result = array_map(function ($i){
            $iArr = explode(',',$i);
            $iArr = array_map(function ($j){
                return (int)$j;
            },$iArr);
//            sort($iArr);
            return StringHelper::jsonEncode($iArr);
        },$result);
        return $result;
    }


    /**
     * 减少最多的sku值
     * @param array $skuList
     * @param array $skuData
     * @return array
     */
    public static function reduceSkuData(array $skuList,array $skuData)
    {
        if(!$skuData){
            return [$skuList,$skuData];
        }
        if(!$skuList){
            return [$skuList,$skuData];
        }
        $skuNumArr = array_map(function ($a){
            return count($a);
        },$skuData);
        $maxNum = max($skuNumArr);
        $delSpecId = null;

        foreach($skuData as $k => $item)
        {
            /**
             * 找到最多的一组sku  将其删除一个
             */
            if(count($item) == $maxNum)
            {
                $delSku = array_pop($skuData[$k]);
                $delSpecId = $delSku['props']['spec_id'];
                break;
            }
        }
        $newList = array();
        foreach($skuList as $s => $sku)
        {
            $specIdList = StringHelper::jsonDecode($sku['spec_id_list']);
            if(in_array($delSpecId,$specIdList))
            {
                unset($skuList[$s]);
            }else{
                $newList [] = $sku;
            }
        }
        return [$newList,$skuData];
    }

    /**
     * 减少最少的sku值
     * @param array $skuList
     * @param array $skuData
     * @return array
     */
    public static function reduceSkuLimitData(array $skuList,array $skuData)
    {
        if(!$skuData){
            return [$skuList,$skuData];
        }
        if(!$skuList){
            return [$skuList,$skuData];
        }
        $max = 0;
        foreach($skuList as $s => $sku)
        {
            $specIdList = StringHelper::jsonDecode($sku['spec_id_list']);
            if(is_array($specIdList)){
                if(count($specIdList)>$max){
                    $max = count($specIdList);
                }
            }
        }
        foreach($skuList as $s => $sku){
            $specIdList = StringHelper::jsonDecode($sku['spec_id_list']);
            if(is_array($specIdList)){
                if(count($specIdList)<$max){
                    unset($skuList[$s]);
                }
            }
        }
        $newList = array();
        foreach($skuList as $s => $sku)
        {
            $newList [] = $sku;
        }
        return [$newList,$skuData];
    }


    /**
     * skuList sku列表过滤
     * @param array $skuList
     * @return array
     */
    public static function tripSkuList(array $skuList)
    {
        foreach($skuList as $k => $sku)
        {
            $properties_name = $sku['properties_name'] ? json_decode($sku['properties_name'], true) : [];

            $tmp = [];
            foreach($properties_name as $properties)
            {
                list($key,$val) = explode(':',$properties);
                //规格名称相似值替换
                $key = PddSpec::transSpecSimilar($key);
                //规格值剔除特殊符号
                $val = PddSpec::skuValueStrip($val);
                array_push($tmp,$key.':'.$val);
            }
            $skuList[$k]['properties_name'] = StringHelper::jsonEncode($tmp);

        }
        return $skuList;
    }



}